package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RestfulController {

    @Autowired
    private User user01;

    @Autowired
    private User user02;

    /**
     * 查询所有用户接口设计
     */
    @GetMapping("/users")
    public List<User> getAllUsers(){

        List<User> users = Arrays.asList(user01, user02);

        return users;
    }

    /**
     * 新增用户接口设计
     * 需要考虑 post请求体中的数据格式
     * 例如：  id=100&name=zhagnsan&salary=1000
     */
    @PostMapping("/users")
    public Map<String,String> addUser(User user) {

        System.out.println(user);
        HashMap<String, String> map = new HashMap<>();

        map.put("code", "1");

        map.put("msg", "新增用户成功");

        return map;
    }

    /**
     * 设计修改用户的接口
     */
    @PutMapping("/users")
    public Map<String,String> updateUser(@RequestBody User user){

        System.out.println(user);

        final HashMap<String, String> map = new HashMap<>();

        map.put("code","1");

        map.put("msg","更新用户成功");

        return map;
    }
    /**
     * 设计删除用户接口
     */
    @DeleteMapping("/users/{id}")
    public  Map<String,String> deleteUser(@PathVariable("id") Integer id){

        System.out.println("删除id为"+id+"的用户");

        HashMap<String, String> map = new HashMap<>();

        map.put("code","1");

        map.put("msg","删除用户成功");

        return map;
    }
}
